<!-- build time:Tue Dec 17 2019 19:26:48 GMT+0800 (GMT+08:00) --><!doctype html><html class="theme-next mist" lang="zh-Hans"><head><meta name="generator" content="Hexo 3.8.0"><meta name="google-site-verification" content="7Tau9WyVgxnsEY9oYedu9g0U6_8akOX3wiKbaYcrg9A"><meta name="baidu-site-verification" content="EVwLiaxdxX"><link href="/css/xps13.css" rel="stylesheet" type="text/css"><link href="/css/message.css" rel="stylesheet" type="text/css"><script type="text/javascript" src="/js/jquery-1.11.3.min.js"></script><meta charset="UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1"><meta http-equiv="Cache-Control" content="no-transform"><meta http-equiv="Cache-Control" content="no-siteapp"><link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css"><link href="/css/main.css?v=5.1.1" rel="stylesheet" type="text/css"><meta name="keywords" content="MongoDB,DataBase,"><link rel="alternate" href="/atom.xml" title="MrBird" type="application/atom+xml"><link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.1"><meta name="description" content="插入文档使用insert向集合中插入一个文档：1234&amp;gt; db.test.insert(&amp;#123;&quot;name&quot;:&quot;mrbird&quot;&amp;#125;)WriteResult(&amp;#123; &quot;nInserted&quot; : 1 &amp;#125;)&amp;gt; db.test.findOne()&amp;#123; &quot;_id&quot; : ObjectId(&quot;58a99b8168e0d7b9f6992c69&quot;), &quot;name&quot; :"><meta name="keywords" content="MongoDB,DataBase"><meta property="og:type" content="article"><meta property="og:title" content="MongoDB文档CUD"><meta property="og:url" content="http://mrbird.cc/MongoDB文档CUD.html"><meta property="og:site_name" content="MrBird"><meta property="og:description" content="插入文档使用insert向集合中插入一个文档：1234&amp;gt; db.test.insert(&amp;#123;&quot;name&quot;:&quot;mrbird&quot;&amp;#125;)WriteResult(&amp;#123; &quot;nInserted&quot; : 1 &amp;#125;)&amp;gt; db.test.findOne()&amp;#123; &quot;_id&quot; : ObjectId(&quot;58a99b8168e0d7b9f6992c69&quot;), &quot;name&quot; :"><meta property="og:locale" content="zh-Hans"><meta property="og:updated_time" content="2019-10-28T12:14:46.251Z"><meta name="twitter:card" content="summary"><meta name="twitter:title" content="MongoDB文档CUD"><meta name="twitter:description" content="插入文档使用insert向集合中插入一个文档：1234&amp;gt; db.test.insert(&amp;#123;&quot;name&quot;:&quot;mrbird&quot;&amp;#125;)WriteResult(&amp;#123; &quot;nInserted&quot; : 1 &amp;#125;)&amp;gt; db.test.findOne()&amp;#123; &quot;_id&quot; : ObjectId(&quot;58a99b8168e0d7b9f6992c69&quot;), &quot;name&quot; :"><script type="text/javascript" id="hexo.configurations">var NexT=window.NexT||{},CONFIG={root:"/",scheme:"Mist",sidebar:{position:"left",display:"always",offset:12,offset_float:0,b2t:!1,scrollpercent:!1},fancybox:!1,motion:!1}</script><title>MongoDB文档CUD | MrBird</title></head><body ondragstart="return!1" class="animsition" lang="zh-Hans" style="overflow-x:hidden;padding-right:280px"><script type="text/javascript" src="/js/mo.min.js"></script><style>@media (min-width:768px) and (max-width:991px){body .header-innerr{width:700px!important}}.header-innerr{margin:0 auto;padding:100px 0 70px;width:880px}@media (min-width:1600px){.container .header-innerr{width:1200px}}.header-innerr{position:relative}.header-innerr{padding:20px 0 0}.header-innerr:after,.header-innerr:before{content:" ";display:table}.header-innerr:after{clear:both}@media (max-width:767px){.header-innerr{width:auto;padding:10px;margin-bottom:-20px}}</style><div class="container sidebar-position-left page-post-detail"><div class="headband"></div><header id="header" class="header"><div class="header-inner"><div class="site-brand-wrapper"><div class="site-meta"><link href="https://fonts.font.im/css?family=Merienda" rel="stylesheet"><div class="custom-logo-site-title"><a href="/" class="brand" rel="start"><span class="logo-line-before"><i></i></span> <span class="site-title" style="font-family:Merienda;font-size:1.3rem">MrBird</span> <span class="logo-line-after"><i></i></span></a></div><p class="site-subtitle"></p></div><div class="site-nav-toggle"><button><span class="btn-bar"></span> <span class="btn-bar"></span> <span class="btn-bar"></span></button></div></div><nav class="site-nav"><ul id="menu" class="menu"><li class="menu-item menu-item-home"><a href="/" rel="section">HOME</a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section">ARCHIVES</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section">TAGS</a></li><li class="menu-item menu-item-friends"><a href="/friends/" rel="section">FRIENDS</a></li><div class="sidebar-toggle" style="display:none"><div class="sidebar-toggle-line-wrap"><span class="sidebar-toggle-line sidebar-toggle-line-first"></span> <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span> <span class="sidebar-toggle-line sidebar-toggle-line-last"></span></div></div></ul><div class="site-search"><div class="popup search-popup local-search-popup"><div class="local-search-header clearfix"><span class="search-icon"><i class="fa fa-search"></i> </span><span class="popup-btn-close"><i class="fa fa-times-circle"></i></span><div class="local-search-input-wrapper"><input autocomplete="off" placeholder="Search" spellcheck="false" type="text" id="local-search-input"></div></div><div id="local-search-result"></div></div></div></nav></div><div class="header-innerr"><div class="note info" style="margin:0;letter-spacing:.15px">🐤手把手教你搭建<strong>Spring Cloud微服务权限系统</strong>（从零到部署）：<a style="color:#40dab2;font-weight:600" href="https://www.kancloud.cn/mrbird/spring-cloud" target="_blank">https://www.kancloud.cn/mrbird/spring-cloud</a></div></div></header><main id="main" class="main"><div class="main-inner"><div class="content-wrap"><div id="content" class="content"><div id="posts" class="posts-expand"><article class="post post-type-normal" itemscope itemtype="http://schema.org/Article"><link itemprop="mainEntityOfPage" href="http://mrbird.cc/MongoDB文档CUD.html"><span hidden itemprop="author" itemscope itemtype="http://schema.org/Person"><meta itemprop="name" content="MrBird"><meta itemprop="description" content=""><meta itemprop="image" content="/images/blogImage.jpg"></span><span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization"><meta itemprop="name" content="MrBird"></span><header class="post-header"><h1 class="post-title" itemprop="name headline">MongoDB文档CUD</h1><div class="post-meta"><span class="post-time"><span class="post-meta-item-icon"><i class="fa fa-calendar-o"></i> </span><span class="post-meta-item-text">Posted on</span> <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-02-19T14:38:57+08:00">2017-02-19 </time></span><span></span> <span class="post-meta-divider">|</span> <span class="page-pv"><i class="fa fa-laptop"></i>&nbsp;&nbsp;Visit count <span class="busuanzi-value" id="busuanzi_value_page_pv"></span></span></div></header><div class="post-body" itemprop="articleBody"><h2 id="插入文档"><a href="#插入文档" class="headerlink" title="插入文档"></a>插入文档</h2><p>使用<code>insert</code>向集合中插入一个文档：</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&gt; db.test.insert(&#123;<span class="string">"name"</span>:<span class="string">"mrbird"</span>&#125;)</span><br><span class="line">WriteResult(&#123; <span class="string">"nInserted"</span> : 1 &#125;)</span><br><span class="line">&gt; db.test.findOne()</span><br><span class="line">&#123; <span class="string">"_id"</span> : ObjectId(<span class="string">"58a99b8168e0d7b9f6992c69"</span>), <span class="string">"name"</span> : <span class="string">"mrbird"</span> &#125;</span><br></pre></td></tr></table></figure><p></p><p>插入的文档没有“_id”键的话，这个操作会自动为文档添加一个“_id”键。<a id="more"></a> 批量插入文档则需使用<code>insertMany</code>函数，函数接收一个文档数组：</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">&gt; db.test.insertMany([&#123;<span class="string">"name"</span>:<span class="string">"Jane"</span>&#125;,&#123;<span class="string">"name"</span>:<span class="string">"KangKang"</span>&#125;])</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">"acknowledged"</span> : <span class="literal">true</span>,</span><br><span class="line">    <span class="string">"insertedIds"</span> : [</span><br><span class="line">        ObjectId(<span class="string">"58a99d6468e0d7b9f6992c6b"</span>),</span><br><span class="line">        ObjectId(<span class="string">"58a99d6468e0d7b9f6992c6c"</span>)</span><br><span class="line">    ]</span><br><span class="line">&#125;</span><br><span class="line">&gt; db.test.find()</span><br><span class="line">&#123; <span class="string">"_id"</span> : ObjectId(<span class="string">"58a99b8168e0d7b9f6992c69"</span>), <span class="string">"name"</span> : <span class="string">"mrbird"</span> &#125;</span><br><span class="line">&#123; <span class="string">"_id"</span> : ObjectId(<span class="string">"58a99d6468e0d7b9f6992c6b"</span>), <span class="string">"name"</span> : <span class="string">"Jane"</span> &#125;</span><br><span class="line">&#123; <span class="string">"_id"</span> : ObjectId(<span class="string">"58a99d6468e0d7b9f6992c6c"</span>), <span class="string">"name"</span> : <span class="string">"KangKang"</span> &#125;</span><br></pre></td></tr></table></figure><p></p><p>要查看一个文档的大小，可以使用<code>Object.bsonsize(doc)</code>函数（单位为字节）：</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">&gt; Object.bsonsize(db.test.find(&#123;<span class="string">"name"</span>:<span class="string">"mrbird"</span>&#125;))</span><br><span class="line">1215</span><br></pre></td></tr></table></figure><p></p><h2 id="删除文档"><a href="#删除文档" class="headerlink" title="删除文档"></a>删除文档</h2><p>删除文档使用<code>remove</code>函数，接收一个查询文档，所有匹配的文档都将会被删除：</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&gt; db.test.remove(&#123;<span class="string">"name"</span>:<span class="string">"mrbird"</span>&#125;)</span><br><span class="line">WriteResult(&#123; <span class="string">"nRemoved"</span> : 1 &#125;)</span><br><span class="line">&gt; db.test.find()</span><br><span class="line">&#123; <span class="string">"_id"</span> : ObjectId(<span class="string">"58a99d6468e0d7b9f6992c6b"</span>), <span class="string">"name"</span> : <span class="string">"Jane"</span> &#125;</span><br><span class="line">&#123; <span class="string">"_id"</span> : ObjectId(<span class="string">"58a99d6468e0d7b9f6992c6c"</span>), <span class="string">"name"</span> : <span class="string">"KangKang"</span> &#125;</span><br></pre></td></tr></table></figure><p></p><p>要清空整个集合的话，可以使用<code>drop</code>函数：</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">&gt; db.test.drop()</span><br><span class="line"><span class="literal">true</span></span><br><span class="line">&gt; db.test.find()</span><br></pre></td></tr></table></figure><p></p><h2 id="更新文档"><a href="#更新文档" class="headerlink" title="更新文档"></a>更新文档</h2><p>使用<code>update</code>函数更新文档，接收两个参数，查询文档和修改器文档，如：</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">&gt; db.test.findOne(&#123;<span class="string">"name"</span>:<span class="string">"mrbird"</span>&#125;)</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">"_id"</span> : ObjectId(<span class="string">"58a9ace92363ff29a7d881e9"</span>),</span><br><span class="line">    <span class="string">"name"</span> : <span class="string">"mrbird"</span>,</span><br><span class="line">    <span class="string">"blog"</span> : <span class="string">"mrbird.leanote.com"</span></span><br><span class="line">&#125;</span><br><span class="line">&gt; var mrbird = db.test.findOne(&#123;<span class="string">"name"</span>:<span class="string">"mrbird"</span>&#125;)</span><br><span class="line">&gt; mrbird.blog = <span class="string">"mrbird's blog"</span></span><br><span class="line">mrbird<span class="string">'s blog</span></span><br><span class="line"><span class="string">&gt; db.test.update(&#123;"name":"mrbird"&#125;,mrbird)</span></span><br><span class="line"><span class="string">WriteResult(&#123; "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 &#125;)</span></span><br><span class="line"><span class="string">&gt; db.test.findOne(&#123;"name":"mrbird"&#125;)</span></span><br><span class="line"><span class="string">&#123;</span></span><br><span class="line"><span class="string">    "_id" : ObjectId("58a9ace92363ff29a7d881e9"),</span></span><br><span class="line"><span class="string">    "name" : "mrbird",</span></span><br><span class="line"><span class="string">    "blog" : "mrbird'</span>s blog<span class="string">"</span></span><br><span class="line"><span class="string">&#125;</span></span><br></pre></td></tr></table></figure><p></p><p><code>$inc</code>修改器用来增加或减少已有的键值，如果该键不存在则创造一个。比如：</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"> &gt; db.test.findOne(&#123;<span class="string">"name"</span>:<span class="string">"mrbird"</span>&#125;)</span><br><span class="line">    &#123;</span><br><span class="line">    <span class="string">"_id"</span> : ObjectId(<span class="string">"58a9ace92363ff29a7d881e9"</span>),</span><br><span class="line">    <span class="string">"name"</span> : <span class="string">"mrbird"</span>,</span><br><span class="line">    <span class="string">"blog"</span> : <span class="string">"mrbird's blog"</span></span><br><span class="line">&#125;</span><br><span class="line">&gt; db.test.update(&#123;<span class="string">"name"</span>:<span class="string">"mrbird"</span>&#125;,&#123;<span class="string">"<span class="variable">$inc</span>"</span>:&#123;<span class="string">"pageview"</span>:1&#125;&#125;)</span><br><span class="line">WriteResult(&#123; <span class="string">"nMatched"</span> : 1, <span class="string">"nUpserted"</span> : 0, <span class="string">"nModified"</span> : 1 &#125;)</span><br><span class="line">&gt; db.test.findOne(&#123;<span class="string">"name"</span>:<span class="string">"mrbird"</span>&#125;)</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">"_id"</span> : ObjectId(<span class="string">"58a9ace92363ff29a7d881e9"</span>),</span><br><span class="line">    <span class="string">"name"</span> : <span class="string">"mrbird"</span>,</span><br><span class="line">    <span class="string">"blog"</span> : <span class="string">"mrbird's blog"</span>,</span><br><span class="line">    <span class="string">"pageview"</span> : 1</span><br><span class="line">&#125;</span><br><span class="line">&gt; db.test.update(&#123;<span class="string">"name"</span>:<span class="string">"mrbird"</span>&#125;,&#123;<span class="string">"<span class="variable">$inc</span>"</span>:&#123;<span class="string">"pageview"</span>:100&#125;&#125;)</span><br><span class="line">WriteResult(&#123; <span class="string">"nMatched"</span> : 1, <span class="string">"nUpserted"</span> : 0, <span class="string">"nModified"</span> : 1 &#125;)</span><br><span class="line">&gt; db.test.findOne(&#123;<span class="string">"name"</span>:<span class="string">"mrbird"</span>&#125;)</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">"_id"</span> : ObjectId(<span class="string">"58a9ace92363ff29a7d881e9"</span>),</span><br><span class="line">    <span class="string">"name"</span> : <span class="string">"mrbird"</span>,</span><br><span class="line">    <span class="string">"blog"</span> : <span class="string">"mrbird's blog"</span>,</span><br><span class="line">    <span class="string">"pageview"</span> : 101</span><br><span class="line">&#125;</span><br><span class="line">&gt; db.test.update(&#123;<span class="string">"name"</span>:<span class="string">"mrbird"</span>&#125;,&#123;<span class="string">"<span class="variable">$inc</span>"</span>:&#123;<span class="string">"pageview"</span>:-50&#125;&#125;)</span><br><span class="line">WriteResult(&#123; <span class="string">"nMatched"</span> : 1, <span class="string">"nUpserted"</span> : 0, <span class="string">"nModified"</span> : 1 &#125;)</span><br><span class="line">&gt; db.test.findOne(&#123;<span class="string">"name"</span>:<span class="string">"mrbird"</span>&#125;)</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">"_id"</span> : ObjectId(<span class="string">"58a9ace92363ff29a7d881e9"</span>),</span><br><span class="line">    <span class="string">"name"</span> : <span class="string">"mrbird"</span>,</span><br><span class="line">    <span class="string">"blog"</span> : <span class="string">"mrbird's blog"</span>,</span><br><span class="line">    <span class="string">"pageview"</span> : 51</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p></p><p><code>$inc</code>只能用于整型，长整型或双精度浮点型的值。</p><p><code>$set</code>用于修改文档的字段值，当这个字段不存在的时候就创建一个。如：</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">&gt; db.test.findOne(&#123;<span class="string">"name"</span>:<span class="string">"mrbird"</span>&#125;)</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">"_id"</span> : ObjectId(<span class="string">"58a9ace92363ff29a7d881e9"</span>),</span><br><span class="line">    <span class="string">"name"</span> : <span class="string">"mrbird"</span>,</span><br><span class="line">    <span class="string">"blog"</span> : <span class="string">"mrbird's blog"</span>,</span><br><span class="line">    <span class="string">"pageview"</span> : 51</span><br><span class="line">&#125;</span><br><span class="line">&gt; db.test.update(&#123;<span class="string">"_id"</span>:ObjectId(<span class="string">"58a9ace92363ff29a7d881e9"</span>)&#125;, </span><br><span class="line">... &#123;<span class="string">"<span class="variable">$set</span>"</span>:&#123;<span class="string">"note"</span>:<span class="string">"love leanote!!"</span>&#125;&#125;)</span><br><span class="line">WriteResult(&#123; <span class="string">"nMatched"</span> : 1, <span class="string">"nUpserted"</span> : 0, <span class="string">"nModified"</span> : 1 &#125;)</span><br><span class="line">&gt; db.test.findOne(&#123;<span class="string">"_id"</span>:ObjectId(<span class="string">"58a9ace92363ff29a7d881e9"</span>)&#125;)</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">"_id"</span> : ObjectId(<span class="string">"58a9ace92363ff29a7d881e9"</span>),</span><br><span class="line">    <span class="string">"name"</span> : <span class="string">"mrbird"</span>,</span><br><span class="line">    <span class="string">"blog"</span> : <span class="string">"mrbird's blog"</span>,</span><br><span class="line">    <span class="string">"pageview"</span> : 51,</span><br><span class="line">    <span class="string">"note"</span> : <span class="string">"love leanote!!"</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p></p><p><code>$set</code>还可以修改键的类型，比如将note键的值改为数组类型：</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">&gt; db.test.update(&#123;<span class="string">"_id"</span>:ObjectId(<span class="string">"58a9ace92363ff29a7d881e9"</span>)&#125;,</span><br><span class="line">... &#123;<span class="string">"<span class="variable">$set</span>"</span>:&#123;<span class="string">"note"</span>:[<span class="string">"love leanote"</span>,<span class="string">"the fun of code"</span>]&#125;&#125;)</span><br><span class="line">WriteResult(&#123; <span class="string">"nMatched"</span> : 1, <span class="string">"nUpserted"</span> : 0, <span class="string">"nModified"</span> : 1 &#125;)</span><br><span class="line">&gt; db.test.findOne(&#123;<span class="string">"_id"</span>:ObjectId(<span class="string">"58a9ace92363ff29a7d881e9"</span>)&#125;)</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">"_id"</span> : ObjectId(<span class="string">"58a9ace92363ff29a7d881e9"</span>),</span><br><span class="line">    <span class="string">"name"</span> : <span class="string">"mrbird"</span>,</span><br><span class="line">    <span class="string">"blog"</span> : <span class="string">"mrbird's blog"</span>,</span><br><span class="line">    <span class="string">"pageview"</span> : 51,</span><br><span class="line">    <span class="string">"note"</span> : [</span><br><span class="line">        <span class="string">"love leanote"</span>,</span><br><span class="line">        <span class="string">"the fun of code"</span></span><br><span class="line">    ]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p></p><p><code>$set</code>也可以修改内嵌文档，比如：</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">&gt; db.blog.findOne()</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">"_id"</span> : ObjectId(<span class="string">"58aa47f645b899838bfb6114"</span>),</span><br><span class="line">    <span class="string">"name"</span> : <span class="string">"mrbird's blog"</span>,</span><br><span class="line">    <span class="string">"post"</span> : <span class="string">"mongodb"</span>,</span><br><span class="line">    <span class="string">"comment"</span> : &#123;</span><br><span class="line">        <span class="string">"name"</span> : <span class="string">"xiaohema"</span>,</span><br><span class="line">        <span class="string">"msg"</span> : <span class="string">"学习了，感谢分享"</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">&gt; db.blog.update(&#123;<span class="string">"name"</span>:<span class="string">"mrbird's blog"</span>&#125;,</span><br><span class="line">... &#123;<span class="string">"<span class="variable">$set</span>"</span>:&#123;<span class="string">"comment.msg"</span>:<span class="string">"好，支持威武有希望了"</span>&#125;&#125;)</span><br><span class="line">&gt; db.blog.findOne()</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">"_id"</span> : ObjectId(<span class="string">"58aa47f645b899838bfb6114"</span>),</span><br><span class="line">    <span class="string">"name"</span> : <span class="string">"mrbird's blog"</span>,</span><br><span class="line">    <span class="string">"post"</span> : <span class="string">"mongodb"</span>,</span><br><span class="line">    <span class="string">"comment"</span> : &#123;</span><br><span class="line">        <span class="string">"name"</span> : <span class="string">"xiaohema"</span>,</span><br><span class="line">        <span class="string">"msg"</span> : <span class="string">"好，支持威武有希望了"</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p></p><p>使用<code>$unset</code>可删除键，比如：</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">&gt; db.test.update(&#123;<span class="string">"_id"</span>:ObjectId(<span class="string">"58a9ace92363ff29a7d881e9"</span>)&#125;,</span><br><span class="line">... &#123;<span class="string">"<span class="variable">$unset</span>"</span>:&#123;<span class="string">"note"</span>:1&#125;&#125;)</span><br><span class="line">WriteResult(&#123; <span class="string">"nMatched"</span> : 1, <span class="string">"nUpserted"</span> : 0, <span class="string">"nModified"</span> : 1 &#125;)</span><br><span class="line">&gt; db.test.findOne(&#123;<span class="string">"_id"</span>:ObjectId(<span class="string">"58a9ace92363ff29a7d881e9"</span>)&#125;)</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">"_id"</span> : ObjectId(<span class="string">"58a9ace92363ff29a7d881e9"</span>),</span><br><span class="line">    <span class="string">"name"</span> : <span class="string">"mrbird"</span>,</span><br><span class="line">    <span class="string">"blog"</span> : <span class="string">"mrbird's blog"</span>,</span><br><span class="line">    <span class="string">"pageview"</span> : 51</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p></p><h3 id="数组修改器"><a href="#数组修改器" class="headerlink" title="数组修改器"></a>数组修改器</h3><p><code>$push</code>会向已有的数组末尾添加一个值，如果数组不存在，则创建该数组。比如：</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">&gt; db.blog.findOne()</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">"_id"</span> : ObjectId(<span class="string">"58aa47f645b899838bfb6114"</span>),</span><br><span class="line">    <span class="string">"name"</span> : <span class="string">"mrbird's blog"</span>,</span><br><span class="line">    <span class="string">"post"</span> : <span class="string">"mongodb"</span></span><br><span class="line">&#125;</span><br><span class="line">&gt; db.blog.update(&#123;<span class="string">"name"</span>:<span class="string">"mrbird's blog"</span>&#125;, &#123;<span class="string">"<span class="variable">$push</span>"</span>:</span><br><span class="line">...&#123;<span class="string">"comments"</span>: &#123;<span class="string">"name"</span>:<span class="string">"ltsc"</span>,<span class="string">"msg"</span>:<span class="string">"不明觉厉"</span>&#125;&#125;&#125;)</span><br><span class="line">WriteResult(&#123; <span class="string">"nMatched"</span> : 1, <span class="string">"nUpserted"</span> : 0, <span class="string">"nModified"</span> : 1 &#125;)</span><br><span class="line">&gt; db.blog.findOne()</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">"_id"</span> : ObjectId(<span class="string">"58aa47f645b899838bfb6114"</span>),</span><br><span class="line">    <span class="string">"name"</span> : <span class="string">"mrbird's blog"</span>,</span><br><span class="line">    <span class="string">"post"</span> : <span class="string">"mongodb"</span>,</span><br><span class="line">    <span class="string">"comments"</span> : [</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="string">"name"</span> : <span class="string">"ltsc"</span>,</span><br><span class="line">            <span class="string">"msg"</span> : <span class="string">"不明觉厉"</span></span><br><span class="line">        &#125;</span><br><span class="line">    ]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p></p><p>如果要一次性向数组中添加多个值，可以使用<code>$push</code>结合<code>$each</code>修改器。比如：</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">&gt; db.blog.update(&#123;<span class="string">"name"</span>:<span class="string">"mrbird's blog"</span>&#125;,</span><br><span class="line">... &#123;<span class="string">"<span class="variable">$push</span>"</span>:&#123;<span class="string">"comments"</span>:&#123;<span class="string">"<span class="variable">$each</span>"</span>:[</span><br><span class="line">... &#123;<span class="string">"name"</span>:<span class="string">"Althars"</span>,<span class="string">"msg"</span>:<span class="string">"siguoyi"</span>&#125;,</span><br><span class="line">... &#123;<span class="string">"name"</span>:<span class="string">"jint"</span>,<span class="string">"msg"</span>:<span class="string">"厉害了我的哥"</span>&#125;]&#125;&#125;&#125;)</span><br><span class="line">WriteResult(&#123; <span class="string">"nMatched"</span> : 1, <span class="string">"nUpserted"</span> : 0, <span class="string">"nModified"</span> : 1 &#125;)</span><br><span class="line">&gt; db.blog.findOne()</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">"_id"</span> : ObjectId(<span class="string">"58aa47f645b899838bfb6114"</span>),</span><br><span class="line">    <span class="string">"name"</span> : <span class="string">"mrbird's blog"</span>,</span><br><span class="line">    <span class="string">"post"</span> : <span class="string">"mongodb"</span>,</span><br><span class="line">    <span class="string">"comments"</span> : [</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="string">"name"</span> : <span class="string">"ltsc"</span>,</span><br><span class="line">            <span class="string">"msg"</span> : <span class="string">"不明觉厉"</span></span><br><span class="line">        &#125;,</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="string">"name"</span> : <span class="string">"Althars"</span>,</span><br><span class="line">            <span class="string">"msg"</span> : <span class="string">"siguoyi"</span></span><br><span class="line">        &#125;,</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="string">"name"</span> : <span class="string">"jint"</span>,</span><br><span class="line">            <span class="string">"msg"</span> : <span class="string">"厉害了我的哥"</span></span><br><span class="line">        &#125;</span><br><span class="line">    ]</span><br></pre></td></tr></table></figure><p></p><p><code>$slice</code>可以在为数组添加值的时候截取数组，但必须配合<code>$push</code>和<code>$each</code>一起使用，否则报语法错误，比如：</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">&gt; db.user.findOne()</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">"_id"</span> : ObjectId(<span class="string">"58aa5e8a6a294f5543ff66eb"</span>),</span><br><span class="line">    <span class="string">"name"</span> : <span class="string">"KangKang"</span>,</span><br><span class="line">    <span class="string">"sex"</span> : <span class="string">"male"</span>,</span><br><span class="line">    <span class="string">"habbit"</span> : [</span><br><span class="line">        <span class="string">"basketball"</span>,</span><br><span class="line">        <span class="string">"football"</span>,</span><br><span class="line">        <span class="string">"swimming"</span>,</span><br><span class="line">        <span class="string">"running"</span></span><br><span class="line">    ]</span><br><span class="line">&#125;</span><br><span class="line">&gt; db.user.update(&#123;<span class="string">"name"</span>:<span class="string">"KangKang"</span>&#125;,</span><br><span class="line">... &#123;<span class="string">"<span class="variable">$push</span>"</span>:&#123;<span class="string">"habbit"</span>:&#123;<span class="string">"<span class="variable">$each</span>"</span>:</span><br><span class="line">... [<span class="string">"eating"</span>],<span class="string">"<span class="variable">$slice</span>"</span>:-3&#125;&#125;&#125;)</span><br><span class="line">WriteResult(&#123; <span class="string">"nMatched"</span> : 1, <span class="string">"nUpserted"</span> : 0, <span class="string">"nModified"</span> : 1 &#125;)</span><br><span class="line">&gt; db.user.findOne()</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">"_id"</span> : ObjectId(<span class="string">"58aa5e8a6a294f5543ff66eb"</span>),</span><br><span class="line">    <span class="string">"name"</span> : <span class="string">"KangKang"</span>,</span><br><span class="line">    <span class="string">"sex"</span> : <span class="string">"male"</span>,</span><br><span class="line">    <span class="string">"habbit"</span> : [</span><br><span class="line">        <span class="string">"swimming"</span>,</span><br><span class="line">        <span class="string">"running"</span>,</span><br><span class="line">        <span class="string">"eating"</span></span><br><span class="line">    ]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p></p><p>从结果可以看出，<code>$slice</code>截取了数组最新的三个值。注意，<code>$slice</code>的值必须是负整数。</p><p>现在有这么一种情况，<code>$push</code>修改器可以向一个数组中添加重复的值，如：</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">&gt; db.user.findOne(&#123;<span class="string">"name"</span>:<span class="string">"mrbird"</span>&#125;)</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">"_id"</span> : ObjectId(<span class="string">"58aa7cb06a294f5543ff66ec"</span>),</span><br><span class="line">    <span class="string">"name"</span> : <span class="string">"mrbird"</span>,</span><br><span class="line">    <span class="string">"email"</span> : [</span><br><span class="line">        <span class="string">"mrbird@qq.com"</span></span><br><span class="line">    ]</span><br><span class="line">&#125;</span><br><span class="line">&gt; db.user.update(&#123;<span class="string">"name"</span>:<span class="string">"mrbird"</span>&#125;, &#123;<span class="string">"<span class="variable">$push</span>"</span>:&#123;<span class="string">"email"</span>:<span class="string">"mrbird@qq.com"</span>&#125;&#125;)</span><br><span class="line">WriteResult(&#123; <span class="string">"nMatched"</span> : 1, <span class="string">"nUpserted"</span> : 0, <span class="string">"nModified"</span> : 1 &#125;)</span><br><span class="line">&gt; db.user.findOne(&#123;<span class="string">"name"</span>:<span class="string">"mrbird"</span>&#125;)</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">"_id"</span> : ObjectId(<span class="string">"58aa7cb06a294f5543ff66ec"</span>),</span><br><span class="line">    <span class="string">"name"</span> : <span class="string">"mrbird"</span>,</span><br><span class="line">    <span class="string">"email"</span> : [</span><br><span class="line">        <span class="string">"mrbird@qq.com"</span>,</span><br><span class="line">        <span class="string">"mrbird@qq.com"</span></span><br><span class="line">    ]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p></p><p>如果希望数组中添加的值不重复的话，可以使用<code>$addToSet</code>修改器：</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">&gt; db.user.update(&#123;<span class="string">"name"</span>:<span class="string">"mrbird"</span>&#125;, &#123;<span class="string">"<span class="variable">$unset</span>"</span>:&#123;<span class="string">"email"</span>:1&#125;&#125;)</span><br><span class="line">WriteResult(&#123; <span class="string">"nMatched"</span> : 1, <span class="string">"nUpserted"</span> : 0, <span class="string">"nModified"</span> : 1 &#125;)</span><br><span class="line">&gt; db.user.findOne(&#123;<span class="string">"name"</span>:<span class="string">"mrbird"</span>&#125;)</span><br><span class="line">&#123; <span class="string">"_id"</span> : ObjectId(<span class="string">"58aa7cb06a294f5543ff66ec"</span>), <span class="string">"name"</span> : <span class="string">"mrbird"</span> &#125;</span><br><span class="line">&gt; db.user.update(&#123;<span class="string">"name"</span>:<span class="string">"mrbird"</span>&#125;, </span><br><span class="line">... &#123;<span class="string">"<span class="variable">$addToSet</span>"</span>:&#123;<span class="string">"email"</span>:&#123;<span class="string">"<span class="variable">$each</span>"</span>:[<span class="string">"mrbird@qq.com"</span>,<span class="string">"mrbird@qq.com"</span>,<span class="string">"mrbird@gmail.com"</span>]&#125;&#125;&#125;)</span><br><span class="line">WriteResult(&#123; <span class="string">"nMatched"</span> : 1, <span class="string">"nUpserted"</span> : 0, <span class="string">"nModified"</span> : 1 &#125;)</span><br><span class="line">&gt; db.user.findOne(&#123;<span class="string">"name"</span>:<span class="string">"mrbird"</span>&#125;)</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">"_id"</span> : ObjectId(<span class="string">"58aa7cb06a294f5543ff66ec"</span>),</span><br><span class="line">    <span class="string">"name"</span> : <span class="string">"mrbird"</span>,</span><br><span class="line">    <span class="string">"email"</span> : [</span><br><span class="line">        <span class="string">"mrbird@qq.com"</span>,</span><br><span class="line">        <span class="string">"mrbird@gmail.com"</span></span><br><span class="line">    ]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p></p><p>删除数组元素有几种方法，比如<code>$pop</code>，<code>{“$pop”:{&quot;key&quot;:1}}</code>表示从数组尾部删除元素，-1则表示从头部删除：</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">&gt; db.user.findOne(&#123;<span class="string">"name"</span>:<span class="string">"mrbird"</span>&#125;)</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">"_id"</span> : ObjectId(<span class="string">"58aa7cb06a294f5543ff66ec"</span>),</span><br><span class="line">    <span class="string">"name"</span> : <span class="string">"mrbird"</span>,</span><br><span class="line">    <span class="string">"email"</span> : [</span><br><span class="line">        <span class="string">"mrbird@qq.com"</span>,</span><br><span class="line">        <span class="string">"mrbird@gmail.com"</span></span><br><span class="line">    ]</span><br><span class="line">&#125;</span><br><span class="line">&gt; db.user.update(&#123;<span class="string">"name"</span>:<span class="string">"mrbird"</span>&#125;,</span><br><span class="line">... &#123;<span class="string">"<span class="variable">$pop</span>"</span>:&#123;<span class="string">"email"</span>:1&#125;&#125;)</span><br><span class="line">WriteResult(&#123; <span class="string">"nMatched"</span> : 1, <span class="string">"nUpserted"</span> : 0, <span class="string">"nModified"</span> : 1 &#125;)</span><br><span class="line">&gt; db.user.findOne(&#123;<span class="string">"name"</span>:<span class="string">"mrbird"</span>&#125;)</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">"_id"</span> : ObjectId(<span class="string">"58aa7cb06a294f5543ff66ec"</span>),</span><br><span class="line">    <span class="string">"name"</span> : <span class="string">"mrbird"</span>,</span><br><span class="line">    <span class="string">"email"</span> : [</span><br><span class="line">        <span class="string">"mrbird@qq.com"</span></span><br><span class="line">    ]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p></p><p>另外一个删除数组元素的修改器为<code>$pull</code>，该操作符会将所有匹配的元素从数组中删除。比如：</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line">&gt; db.user.update(&#123;<span class="string">"name"</span>:<span class="string">"KangKang"</span>&#125;,</span><br><span class="line">... &#123;<span class="string">"<span class="variable">$push</span>"</span>:&#123;<span class="string">"habbit"</span>:<span class="string">"eating"</span>&#125;&#125;)</span><br><span class="line">WriteResult(&#123; <span class="string">"nMatched"</span> : 1, <span class="string">"nUpserted"</span> : 0, <span class="string">"nModified"</span> : 1 &#125;)</span><br><span class="line">&gt; db.user.findOne(&#123;<span class="string">"name"</span>:<span class="string">"KangKang"</span>&#125;)</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">"_id"</span> : ObjectId(<span class="string">"58aa5e8a6a294f5543ff66eb"</span>),</span><br><span class="line">    <span class="string">"name"</span> : <span class="string">"KangKang"</span>,</span><br><span class="line">    <span class="string">"sex"</span> : <span class="string">"male"</span>,</span><br><span class="line">    <span class="string">"habbit"</span> : [</span><br><span class="line">        <span class="string">"swimming"</span>,</span><br><span class="line">        <span class="string">"running"</span>,</span><br><span class="line">        <span class="string">"eating"</span>,</span><br><span class="line">        <span class="string">"eating"</span></span><br><span class="line">    ]</span><br><span class="line">&#125;</span><br><span class="line">&gt; db.user.update(&#123;<span class="string">"name"</span>:<span class="string">"KangKang"</span>&#125;,</span><br><span class="line">... &#123;<span class="string">"<span class="variable">$pull</span>"</span>:&#123;<span class="string">"habbit"</span>:<span class="string">"eating"</span>&#125;&#125;)</span><br><span class="line">WriteResult(&#123; <span class="string">"nMatched"</span> : 1, <span class="string">"nUpserted"</span> : 0, <span class="string">"nModified"</span> : 1 &#125;)</span><br><span class="line">&gt; db.user.findOne(&#123;<span class="string">"name"</span>:<span class="string">"KangKang"</span>&#125;)</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">"_id"</span> : ObjectId(<span class="string">"58aa5e8a6a294f5543ff66eb"</span>),</span><br><span class="line">    <span class="string">"name"</span> : <span class="string">"KangKang"</span>,</span><br><span class="line">    <span class="string">"sex"</span> : <span class="string">"male"</span>,</span><br><span class="line">    <span class="string">"habbit"</span> : [</span><br><span class="line">        <span class="string">"swimming"</span>,</span><br><span class="line">        <span class="string">"running"</span></span><br><span class="line">    ]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p></p><p>还可以通过数组的下标修改数组内容，如下所示：</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line">&gt; db.blog.find(&#123;<span class="string">"name"</span>:<span class="string">"mrbird's blog"</span>&#125;).pretty()</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">"_id"</span> : ObjectId(<span class="string">"58aa47f645b899838bfb6114"</span>),</span><br><span class="line">    <span class="string">"name"</span> : <span class="string">"mrbird's blog"</span>,</span><br><span class="line">    <span class="string">"post"</span> : <span class="string">"mongodb"</span>,</span><br><span class="line">    <span class="string">"comments"</span> : [</span><br><span class="line">            &#123;</span><br><span class="line">                <span class="string">"name"</span> : <span class="string">"ltsc"</span>,</span><br><span class="line">                <span class="string">"msg"</span> : <span class="string">"不明觉厉"</span></span><br><span class="line">            &#125;,</span><br><span class="line">            &#123;</span><br><span class="line">                <span class="string">"name"</span> : <span class="string">"Althars"</span>,</span><br><span class="line">                <span class="string">"msg"</span> : <span class="string">"siguoyi"</span></span><br><span class="line">            &#125;,</span><br><span class="line">            &#123;</span><br><span class="line">                <span class="string">"name"</span> : <span class="string">"jint"</span>,</span><br><span class="line">                <span class="string">"msg"</span> : <span class="string">"厉害了我的哥"</span></span><br><span class="line">            &#125;</span><br><span class="line">    ]</span><br><span class="line">&#125;</span><br><span class="line">&gt; db.blog.update(&#123;<span class="string">"name"</span>:<span class="string">"mrbird's blog"</span>&#125;,</span><br><span class="line">... &#123;<span class="string">"<span class="variable">$set</span>"</span>:&#123;<span class="string">"comments.1.msg"</span>:<span class="string">"四国以"</span>&#125;&#125;)</span><br><span class="line">WriteResult(&#123; <span class="string">"nMatched"</span> : 1, <span class="string">"nUpserted"</span> : 0, <span class="string">"nModified"</span> : 1 &#125;)</span><br><span class="line">&gt; db.blog.find(&#123;<span class="string">"name"</span>:<span class="string">"mrbird's blog"</span>&#125;).pretty()</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">"_id"</span> : ObjectId(<span class="string">"58aa47f645b899838bfb6114"</span>),</span><br><span class="line">    <span class="string">"name"</span> : <span class="string">"mrbird's blog"</span>,</span><br><span class="line">    <span class="string">"post"</span> : <span class="string">"mongodb"</span>,</span><br><span class="line">    <span class="string">"comments"</span> : [</span><br><span class="line">            &#123;</span><br><span class="line">                <span class="string">"name"</span> : <span class="string">"ltsc"</span>,</span><br><span class="line">                <span class="string">"msg"</span> : <span class="string">"不明觉厉"</span></span><br><span class="line">            &#125;,</span><br><span class="line">            &#123;</span><br><span class="line">                <span class="string">"name"</span> : <span class="string">"Althars"</span>,</span><br><span class="line">                <span class="string">"msg"</span> : <span class="string">"四国以"</span></span><br><span class="line">            &#125;,</span><br><span class="line">            &#123;</span><br><span class="line">                <span class="string">"name"</span> : <span class="string">"jint"</span>,</span><br><span class="line">                <span class="string">"msg"</span> : <span class="string">"厉害了我的哥"</span></span><br><span class="line">            &#125;</span><br><span class="line">    ]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p></p><p>这种做法有局限性，就是必须先知道待修改字段的数组下标，可以使用另外一种方法，下面这种方法只需要知道待修改字段就行了：</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">&gt; db.blog.update(&#123;<span class="string">"comments.msg"</span>:<span class="string">"四国以"</span>&#125;,</span><br><span class="line">... &#123;<span class="string">"<span class="variable">$set</span>"</span>:&#123;<span class="string">"comments.$.msg"</span>:<span class="string">"看完此文，犹如醍醐灌顶"</span>&#125;&#125;)</span><br><span class="line">WriteResult(&#123; <span class="string">"nMatched"</span> : 1, <span class="string">"nUpserted"</span> : 0, <span class="string">"nModified"</span> : 1 &#125;)</span><br><span class="line">&gt; db.blog.find(&#123;<span class="string">"name"</span>:<span class="string">"mrbird's blog"</span>&#125;).pretty()</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">"_id"</span> : ObjectId(<span class="string">"58aa47f645b899838bfb6114"</span>),</span><br><span class="line">    <span class="string">"name"</span> : <span class="string">"mrbird's blog"</span>,</span><br><span class="line">    <span class="string">"post"</span> : <span class="string">"mongodb"</span>,</span><br><span class="line">    <span class="string">"comments"</span> : [</span><br><span class="line">            &#123;</span><br><span class="line">                <span class="string">"name"</span> : <span class="string">"ltsc"</span>,</span><br><span class="line">                <span class="string">"msg"</span> : <span class="string">"不明觉厉"</span></span><br><span class="line">            &#125;,</span><br><span class="line">            &#123;</span><br><span class="line">                <span class="string">"name"</span> : <span class="string">"Althars"</span>,</span><br><span class="line">                <span class="string">"msg"</span> : <span class="string">"看完此文，犹如醍醐灌顶"</span></span><br><span class="line">            &#125;,</span><br><span class="line">            &#123;</span><br><span class="line">                <span class="string">"name"</span> : <span class="string">"jint"</span>,</span><br><span class="line">                <span class="string">"msg"</span> : <span class="string">"厉害了我的哥"</span></span><br><span class="line">            &#125;</span><br><span class="line">    ]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p></p><p><code>update</code>函数的第三个参数为<code>upsert</code>，设置为<code>true</code>时，新一个文档，没有找到匹配的查询文档时，插入该文档，找到了就更新，比如：</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">&gt; db.blog.findOne(&#123;<span class="string">"name"</span>:<span class="string">"mrbrid"</span>,<span class="string">"post"</span>:<span class="string">"MongoDB文档CUD"</span>&#125;)</span><br><span class="line">null</span><br><span class="line">&gt; db.blog.update(&#123;<span class="string">"name"</span>:<span class="string">"mrbrid"</span>,<span class="string">"post"</span>:<span class="string">"MongoDB文档CUD"</span>&#125;,</span><br><span class="line">... &#123;<span class="string">"<span class="variable">$inc</span>"</span>:&#123;<span class="string">"pageview"</span>:1&#125;&#125;,<span class="literal">true</span>)</span><br><span class="line">WriteResult(&#123;</span><br><span class="line">    <span class="string">"nMatched"</span> : 0,</span><br><span class="line">    <span class="string">"nUpserted"</span> : 1,</span><br><span class="line">    <span class="string">"nModified"</span> : 0,</span><br><span class="line">    <span class="string">"_id"</span> : ObjectId(<span class="string">"58acf0743c8ad0b0d9d65f45"</span>)</span><br><span class="line">&#125;)</span><br><span class="line">&gt; db.blog.findOne(&#123;<span class="string">"name"</span>:<span class="string">"mrbrid"</span>,<span class="string">"post"</span>:<span class="string">"MongoDB文档CUD"</span>&#125;)</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">"_id"</span> : ObjectId(<span class="string">"58acf0743c8ad0b0d9d65f45"</span>),</span><br><span class="line">    <span class="string">"name"</span> : <span class="string">"mrbrid"</span>,</span><br><span class="line">    <span class="string">"post"</span> : <span class="string">"MongoDB文档CUD"</span>,</span><br><span class="line">    <span class="string">"pageview"</span> : 1</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p></p><p>update函数的第四个参数为<code>multi</code>，设置为<code>true</code>的时候，批量更新和查询文档匹配的文档，比如将mrbird’s blog集合中所有文档的pageview增加1：</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line">&gt; db.blog.find().pretty()</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">"_id"</span> : ObjectId(<span class="string">"58acf0743c8ad0b0d9d65f45"</span>),</span><br><span class="line">    <span class="string">"name"</span> : <span class="string">"mrbird's blog"</span>,</span><br><span class="line">    <span class="string">"post"</span> : <span class="string">"MongoDB文档CUD"</span>,</span><br><span class="line">    <span class="string">"pageview"</span> : 1</span><br><span class="line">&#125;</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">"_id"</span> : ObjectId(<span class="string">"58acf64b3c8ad0b0d9d65f4a"</span>),</span><br><span class="line">    <span class="string">"name"</span> : <span class="string">"mrbird's blog"</span>,</span><br><span class="line">    <span class="string">"post"</span> : <span class="string">"MongoDB shell"</span>,</span><br><span class="line">    <span class="string">"pageview"</span> : 1</span><br><span class="line">&#125;</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">"_id"</span> : ObjectId(<span class="string">"58acf65d3c8ad0b0d9d65f4d"</span>),</span><br><span class="line">    <span class="string">"name"</span> : <span class="string">"mrbird's blog"</span>,</span><br><span class="line">    <span class="string">"post"</span> : <span class="string">"start Spring Boot"</span>,</span><br><span class="line">    <span class="string">"pageview"</span> : 1</span><br><span class="line">&#125;</span><br><span class="line">&gt; db.blog.update(&#123;<span class="string">"name"</span>:<span class="string">"mrbird's blog"</span>&#125;,&#123;<span class="string">"<span class="variable">$inc</span>"</span>:&#123;<span class="string">"pageview"</span>:1&#125;&#125;,</span><br><span class="line">... <span class="literal">false</span>,<span class="literal">true</span>)</span><br><span class="line">WriteResult(&#123; <span class="string">"nMatched"</span> : 3, <span class="string">"nUpserted"</span> : 0, <span class="string">"nModified"</span> : 3 &#125;)</span><br><span class="line">&gt; db.blog.find().pretty()</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">"_id"</span> : ObjectId(<span class="string">"58acf0743c8ad0b0d9d65f45"</span>),</span><br><span class="line">    <span class="string">"name"</span> : <span class="string">"mrbird's blog"</span>,</span><br><span class="line">    <span class="string">"post"</span> : <span class="string">"MongoDB文档CUD"</span>,</span><br><span class="line">    <span class="string">"pageview"</span> : 2</span><br><span class="line">&#125;</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">"_id"</span> : ObjectId(<span class="string">"58acf64b3c8ad0b0d9d65f4a"</span>),</span><br><span class="line">    <span class="string">"name"</span> : <span class="string">"mrbird's blog"</span>,</span><br><span class="line">    <span class="string">"post"</span> : <span class="string">"MongoDB shell"</span>,</span><br><span class="line">    <span class="string">"pageview"</span> : 2</span><br><span class="line">&#125;</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">"_id"</span> : ObjectId(<span class="string">"58acf65d3c8ad0b0d9d65f4d"</span>),</span><br><span class="line">    <span class="string">"name"</span> : <span class="string">"mrbird's blog"</span>,</span><br><span class="line">    <span class="string">"post"</span> : <span class="string">"start Spring Boot"</span>,</span><br><span class="line">    <span class="string">"pageview"</span> : 2</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p></p><p>另外，调用<code>getLastError</code>可查看最近一次更新的文档数量，如：</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">&gt; db.runCommand(&#123;getLastError:1&#125;)</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">"connectionId"</span> : 1,</span><br><span class="line">    <span class="string">"updatedExisting"</span> : <span class="literal">true</span>,</span><br><span class="line">    <span class="string">"n"</span> : 3,</span><br><span class="line">    <span class="string">"syncMillis"</span> : 0,</span><br><span class="line">    <span class="string">"writtenTo"</span> : null,</span><br><span class="line">    <span class="string">"err"</span> : null,</span><br><span class="line">    <span class="string">"ok"</span> : 1</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p></p><p>拥有类似事务特性的更新与查询操作<code>findAndModify</code>。它是原子性的，会返回符合查询条件的更新后的文档。一次最多只更新一个文档，也就是条件<code>query</code>条件，且执行<code>sort</code>后的第一个文档。语法如下：</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">db.COLLECTION_NAME.findAndModify(&#123;</span><br><span class="line">    query:&#123;&#125;,</span><br><span class="line">    update:&#123;&#125;,</span><br><span class="line">    remove:<span class="literal">true</span>|<span class="literal">false</span>,</span><br><span class="line">    new:<span class="literal">true</span>|<span class="literal">false</span>,</span><br><span class="line">    sort:&#123;&#125;,</span><br><span class="line">    fields:&#123;&#125;,</span><br><span class="line">    upsert:<span class="literal">true</span>|<span class="literal">false</span>&#125;</span><br><span class="line">);﻿​</span><br></pre></td></tr></table></figure><p></p><ol><li><p>query是查询选择器，与findOne的查询选择器相同。</p></li><li><p>update是要更新的值，不能与remove同时出现。</p></li><li><p>remove表示删除符合query条件的文档，不能与update同时出现。</p></li><li><p>new为true：返回更新后的文档，false：返回更新前的，默认是false。</p></li><li><p>sort：排序条件，与sort函数的参数一致。</p></li><li><p>fields:投影操作，与find的第二个参数一致。</p></li><li><p>upsert:与update的upsert参数一样。</p></li></ol><p>例子：</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line">&gt; db.blog.findAndModify(&#123;</span><br><span class="line">... <span class="string">"query"</span>:&#123;<span class="string">"name"</span>:<span class="string">"mrbird's blog"</span>&#125;,</span><br><span class="line">... <span class="string">"update"</span>:&#123;</span><br><span class="line">... <span class="string">"<span class="variable">$inc</span>"</span>:&#123;<span class="string">"pageview"</span>:1&#125;,</span><br><span class="line">... <span class="string">"<span class="variable">$set</span>"</span>:&#123;<span class="string">"like"</span>:1&#125;&#125;&#125;)</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">"_id"</span> : ObjectId(<span class="string">"58acf0743c8ad0b0d9d65f45"</span>),</span><br><span class="line">    <span class="string">"name"</span> : <span class="string">"mrbird's blog"</span>,</span><br><span class="line">    <span class="string">"post"</span> : <span class="string">"MongoDB文档CUD"</span>,</span><br><span class="line">    <span class="string">"pageview"</span> : 3</span><br><span class="line">&#125;</span><br><span class="line">&gt; db.blog.find().pretty()</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">"_id"</span> : ObjectId(<span class="string">"58acf0743c8ad0b0d9d65f45"</span>),</span><br><span class="line">    <span class="string">"name"</span> : <span class="string">"mrbird's blog"</span>,</span><br><span class="line">    <span class="string">"post"</span> : <span class="string">"MongoDB文档CUD"</span>,</span><br><span class="line">    <span class="string">"pageview"</span> : 4,</span><br><span class="line">    <span class="string">"like"</span> : 1</span><br><span class="line">&#125;</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">"_id"</span> : ObjectId(<span class="string">"58acf64b3c8ad0b0d9d65f4a"</span>),</span><br><span class="line">    <span class="string">"name"</span> : <span class="string">"mrbird's blog"</span>,</span><br><span class="line">    <span class="string">"post"</span> : <span class="string">"MongoDB shell"</span>,</span><br><span class="line">    <span class="string">"pageview"</span> : 3</span><br><span class="line">&#125;</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">"_id"</span> : ObjectId(<span class="string">"58acf65d3c8ad0b0d9d65f4d"</span>),</span><br><span class="line">    <span class="string">"name"</span> : <span class="string">"mrbird's blog"</span>,</span><br><span class="line">    <span class="string">"post"</span> : <span class="string">"start Spring Boot"</span>,</span><br><span class="line">    <span class="string">"pageview"</span> : 3</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p></p><p>可发现，执行<code>findAndModify</code>后，返回被更新前（默认显示更新前的）的文档，并且只更新了匹配的第一条文档。</p><p>如果要返回被更新后的文档，我们设置new 为true：</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">&gt; db.blog.findAndModify(&#123; </span><br><span class="line">...<span class="string">"query"</span>:&#123;<span class="string">"name"</span>:<span class="string">"mrbird's blog"</span>&#125;, </span><br><span class="line">...<span class="string">"update"</span>:&#123;  <span class="string">"<span class="variable">$inc</span>"</span>:&#123;<span class="string">"pageview"</span>:1&#125;, <span class="string">"<span class="variable">$set</span>"</span>:&#123;<span class="string">"like"</span>:2&#125;&#125;,</span><br><span class="line">...<span class="string">"new"</span>:<span class="literal">true</span>&#125;)</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">"_id"</span> : ObjectId(<span class="string">"58acf0743c8ad0b0d9d65f45"</span>),</span><br><span class="line">    <span class="string">"name"</span> : <span class="string">"mrbird's blog"</span>,</span><br><span class="line">    <span class="string">"post"</span> : <span class="string">"MongoDB文档CUD"</span>,</span><br><span class="line">    <span class="string">"pageview"</span> : 5,</span><br><span class="line">    <span class="string">"like"</span> : 2</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p></p><blockquote><p><a href="https://book.douban.com/subject/25960887/" target="_blank" rel="noopener">《MongoDB权威指南》</a>读书笔记</p></blockquote><script>$(".post-body a").not(".thispage").addClass("ignore-href").attr("target","_blank")</script></div><div></div><div><div style="padding:10px 0;margin:20px auto;width:90%;text-align:center;color:#878787"><div>请作者喝瓶肥宅水~</div><button id="rewardButton" style="margin-top:10px" disable="enable" onclick='var e=document.getElementById("QR");"none"===e.style.display?e.style.display="block":e.style.display="none"'><span style="height:46px;width:46px;line-height:46px;border-radius:50%;color:#fe5f55;font-weight:600;background:#ffd5be;border:none;box-shadow:0 4px 8px 0 rgba(255,213,190,.4)">￥</span></button><div id="QR" style="display:none"><div id="wechat" style="display:inline-block"><img id="wechat_qr" src="/img/wechat_pay.png" alt="MrBird WeChat Pay"></div><div id="alipay" style="display:inline-block"><img id="alipay_qr" src="/img/ali_pay.png" alt="MrBird Alipay"></div></div></div><style>#QR img{width:auto;margin:.8em 1em 0 1em}</style></div><div><ul class="post-copyright"><li class="post-copyright-author"><strong>本文作者：</strong> MrBird</li><li class="post-copyright-link"><strong>本文链接：</strong> <a href="http://mrbird.cc/MongoDB文档CUD.html" title="MongoDB文档CUD">http://mrbird.cc/MongoDB文档CUD.html</a></li><li class="post-copyright-license"><strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" rel="external nofollow" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明出处！</li></ul></div><footer class="post-footer"><div class="post-tags" style="margin-bottom:1.3rem"><a href="/tags/MongoDB/" rel="tag"># MongoDB</a> <a href="/tags/DataBase/" rel="tag"># DataBase</a></div><div class="post-nav"><div class="post-nav-next post-nav-item"><a href="/Leanote博客主题Conciseness.html" rel="next" title="Leanote博客主题Conciseness"><i class="fa fa-chevron-left"></i> Leanote博客主题Conciseness</a></div><span class="post-nav-divider"></span><div class="post-nav-prev post-nav-item"><a href="/MongoDB-文档查询.html" rel="prev" title="MongoDB 文档查询">MongoDB 文档查询 <i class="fa fa-chevron-right"></i></a></div></div></footer></article><hr><div id="container"></div><div class="post-spread"><div id="comment-div"></div><style>.valine .vlist{margin-bottom:3rem}.valine .vwrap .vcontrol .col.col-60{text-align:left}.valine .vlist .vcard .vhead,.valine .vlist .vcard section .vfooter{text-align:left}.valine .vlist .vcard section{padding-bottom:.5rem!important}.vname{color:#42b983!important}.valine .vinfo .col{text-align:left;margin-left:-27rem}div#comment-div{margin-bottom:-8rem}.valine .vlist .vcard .vcontent .code,.valine .vlist .vcard .vcontent code,.valine .vlist .vcard .vcontent pre{background:#fbfbfb}.valine .vlist .vcard .vcontent a{color:#42b983}.valine .vlist .vcard .vimg{border-radius:3px}.valine .vbtn{border-radius:2px;padding:.3rem 1.25rem}.valine .vbtn:active,.valine .vbtn:hover{color:#42b983;border-color:#42b983;background-color:#fff}.valine .vwrap .vheader .vinput:focus{border-bottom-color:#42b983}.valine .vlist .vcard .vcontent.expand:before{background:-webkit-gradient(linear,left top,left bottom,from(hsla(0,0%,100%,0)),to(hsla(0,0%,100%,.2)));background:linear-gradient(180deg,hsla(0,0%,100%,0),hsla(0,0%,100%,.2))}.valine .vlist .vcard .vcontent.expand:after{content:"点击展开";font-size:.4rem;text-align:right;left:-1rem;background:hsla(0,0%,100%,.2)}.valine .vlist .vcard section .vfooter .vat{color:#b3b3b3}.valine .vlist .vcard section .vfooter .vat:hover{color:#42b983}.vcontent img{margin:0}.valine .info{display:none}</style><script type="text/javascript" src="/js/av.min.js"></script><script type="text/javascript" src="/js/Valine.min.js"></script><script type="text/javascript" src="/js/activate-power-mode.js"></script><script>POWERMODE.colorful=!0,POWERMODE.shake=!1,document.body.addEventListener("input",POWERMODE),new Valine({el:"#comment-div",notify:!1,verify:!0,appId:"SMcDFP1bN1jgb9Lo8JmtICHm-gzGzoHsz",appKey:"dH4nrUrt3V5XiJiI6Qyejnbi",placeholder:"",path:window.location.pathname,avatar:"monsterid",guest_info:["nick","mail","link"]});var chicken='<a href="#"><img src="https://image.uisdc.com/wp-content/uploads/2018/06/uisdc-chat-chicken.gif" class="checken"></a>';$("#comment-div").prepend(chicken)</script></div></div><script>var $bqinline=$("img[alt='bq-inline']");$bqinline.css({width:"2.3rem",height:"2.3rem",display:"inline","vertical-align":"text-bottom"})</script></div><div class="comments" id="comments"></div></div><aside id="sidebar" class="sidebar" onselectstart="return!1"><div class="sidebar-inner"><ul class="sidebar-nav motion-element"><li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">Contents</li><li class="sidebar-nav-overview" data-target="site-overview">Site Preview</li></ul><section class="site-overview sidebar-panel"><div class="sidebar-sticky sticky"><div itemscope itemtype="http://schema.org/Person"><div class="author__avatar"><img src="/images/blogImage.jpg" class="author__avatar" alt="MrBird" itemprop="image"></div><div class="author__content"><h3 class="author__name" itemprop="name">MrBird's Blog</h3><p class="author__bio" itemprop="description">A simple blog, code repository, just keep blogging</p></div><div class="author__urls-wrapper"><button class="btn btn--inverse">Follow</button><ul class="author__urls social-icons"><li><a href="http://map.baidu.com/?newmap=1&s=s%26wd%3D%E7%A6%8F%E5%B7%9E%E5%B8%82%26c%3D300&from=alamap&tpl=mapcity" target="_blank" itemprop="url" class="ignore-href"><i class="fa fa-fw fa-map-marker" aria-hidden="true"></i>&nbsp;&nbsp;FuZhou,CN</a></li><li><a href="https://love.mrbird.cc" target="_blank" itemprop="url" class="ignore-href"><i class="fa fa-fw fa-diamond" aria-hidden="true"></i>&nbsp;&nbsp;Love</a></li><li><a href="https://cloud.mrbird.cn" target="_blank" itemprop="url" class="ignore-href"><i class="fa fa-fw fa-chain" aria-hidden="true"></i>&nbsp;&nbsp;FEBS</a></li><li><a href="/atom.xml" target="_blank" itemprop="url" class="ignore-href"><i class="fa fa-fw fa-rss" aria-hidden="true"></i>&nbsp;&nbsp;RSS</a></li><li><a href="https://gitee.com/mrbirdd" target="_blank" itemprop="sameAs" class="ignore-href"><i class="fa fa-fw fa-codepen" aria-hidden="true"></i>&nbsp;&nbsp;Gitee</a></li><li><a href="https://github.com/wuyouzhuguli" target="_blank" itemprop="sameAs" class="ignore-href"><i class="fa fa-fw fa-github-alt" aria-hidden="true"></i>&nbsp;&nbsp;GitHub</a></li><li><a href="javascript:;" class="popup-trigger"><i class="fa fa-fw fa-search" aria-hidden="true"></i>&nbsp;&nbsp;Search</a></li></ul></div></div></div><script>var $urls=$(".author__urls").find("a");$urls.each(function(){var o=$(this);o.mouseenter(function(){o.css({color:"#414547"})}),o.mouseleave(function(){o.css({color:""})})})</script></section><section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active"><div class="post-toc"><div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#插入文档"><span class="nav-number">1.</span> <span class="nav-text">插入文档</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#删除文档"><span class="nav-number">2.</span> <span class="nav-text">删除文档</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#更新文档"><span class="nav-number">3.</span> <span class="nav-text">更新文档</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#数组修改器"><span class="nav-number">3.1.</span> <span class="nav-text">数组修改器</span></a></li></ol></li></ol></div></div></section></div></aside></div></main><footer id="footer" class="footer" onselectstart="return!1"><div class="footer-inner"><div class="copyright">&copy; 2016 - <span itemprop="copyrightYear">2019</span>&nbsp;&nbsp; <span class="author" itemprop="copyrightHolder">MrBird</span>&nbsp;&nbsp;|<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>&nbsp;&nbsp;UV&nbsp;<span class="busuanzi-value" id="busuanzi_value_site_uv" style="cursor:pointer" title="统计开始时间：2019年7月5日"></span> &nbsp;&nbsp;PV&nbsp;<span class="busuanzi-value" id="busuanzi_value_site_pv" style="cursor:pointer" title="统计开始时间：2019年7月5日"></span></div></div></footer><div class="back-to-top"><span style="font-family:'Source Sans Pro','Helvetica Neue',Arial,sans-serif;font-size:1.2em;font-weight:600">TOP</span></div></div><script type="text/javascript">"[object Function]"!==Object.prototype.toString.call(window.Promise)&&(window.Promise=null)</script><script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script><script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script><script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script><script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script><script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script><script type="text/javascript" src="/js/src/utils.js?v=5.1.1"></script><script type="text/javascript" src="/js/src/motion.js?v=5.1.1"></script><script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.1"></script><script type="text/javascript" src="/js/src/post-details.js?v=5.1.1"></script><script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.1"></script><script type="text/javascript">function proceedsearch(){$("body").append('<div class="search-popup-overlay local-search-pop-overlay"></div>').css("overflow","hidden"),$(".search-popup-overlay").click(onPopupClose),$(".popup").toggle();var t=$("#local-search-input");t.attr("autocapitalize","none"),t.attr("autocorrect","off"),t.focus()}var isfetched=!1,isXml=!0,search_path="search.xml";0===search_path.length?search_path="search.xml":search_path.endsWith("json")&&(isXml=!1);var path="/"+search_path,onPopupClose=function(t){$(".popup").hide(),$("#local-search-input").val(""),$(".search-result-list").remove(),$("#no-result").remove(),$(".local-search-pop-overlay").remove(),$("body").css("overflow","")},searchFunc=function(t,e,o){"use strict";$("body").append('<div class="search-popup-overlay local-search-pop-overlay"><div id="search-loading-icon"><i class="fa fa-spinner fa-pulse fa-2x fa-fw"></i></div></div>').css("overflow","hidden"),$("#search-loading-icon").css("margin","20% auto 0 auto").css("text-align","center"),$.ajax({url:t,dataType:isXml?"xml":"json",async:!0,success:function(t){isfetched=!0,$(".popup").detach().appendTo(".header-inner");var n=isXml?$("entry",t).map(function(){return{title:$("title",this).text(),content:$("content",this).text(),url:$("url",this).text()}}).get():t,r=document.getElementById(e),s=document.getElementById(o),a=function(){var t=r.value.trim().toLowerCase(),e=t.split(/[\s\-]+/);e.length>1&&e.push(t);var o=[];if(t.length>0&&n.forEach(function(n){function r(e,o,n,r){for(var s=r[r.length-1],a=s.position,i=s.word,l=[],h=0;a+i.length<=n&&0!=r.length;){i===t&&h++,l.push({position:a,length:i.length});var p=a+i.length;for(r.pop();0!=r.length&&(s=r[r.length-1],a=s.position,i=s.word,p>a);)r.pop()}return c+=h,{hits:l,start:o,end:n,searchTextCount:h}}function s(t,e){var o="",n=e.start;return e.hits.forEach(function(e){o+=t.substring(n,e.position);var r=e.position+e.length;o+='<b class="search-keyword">'+t.substring(e.position,r)+"</b>",n=r}),o+=t.substring(n,e.end)}var a=!1,i=0,c=0,l=n.title.trim(),h=l.toLowerCase(),p=n.content.trim().replace(/<[^>]+>/g,""),u=p.toLowerCase(),f=decodeURIComponent(n.url),d=[],g=[];if(""!=l&&(e.forEach(function(t){function e(t,e,o){var n=t.length;if(0===n)return[];var r=0,s=[],a=[];for(o||(e=e.toLowerCase(),t=t.toLowerCase());(s=e.indexOf(t,r))>-1;)a.push({position:s,word:t}),r=s+n;return a}d=d.concat(e(t,h,!1)),g=g.concat(e(t,u,!1))}),(d.length>0||g.length>0)&&(a=!0,i=d.length+g.length)),a){[d,g].forEach(function(t){t.sort(function(t,e){return e.position!==t.position?e.position-t.position:t.word.length-e.word.length})});var v=[];0!=d.length&&v.push(r(l,0,l.length,d));for(var C=[];0!=g.length;){var $=g[g.length-1],m=$.position,x=$.word,w=m-20,y=m+80;w<0&&(w=0),y<m+x.length&&(y=m+x.length),y>p.length&&(y=p.length),C.push(r(p,w,y,g))}C.sort(function(t,e){return t.searchTextCount!==e.searchTextCount?e.searchTextCount-t.searchTextCount:t.hits.length!==e.hits.length?e.hits.length-t.hits.length:t.start-e.start});var T=parseInt("1");T>=0&&(C=C.slice(0,T));var b="";b+=0!=v.length?"<li><a href='"+f+"' class='search-result-title'>"+s(l,v[0])+"</a>":"<li><a href='"+f+"' class='search-result-title'>"+l+"</a>",C.forEach(function(t){b+="<a href='"+f+'\'><p class="search-result">'+s(p,t)+"...</p></a>"}),b+="</li>",o.push({item:b,searchTextCount:c,hitCount:i,id:o.length})}}),1===e.length&&""===e[0])s.innerHTML='<div id="no-result"><i class="fa fa-search fa-5x" /></div>';else if(0===o.length)s.innerHTML='<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>';else{o.sort(function(t,e){return t.searchTextCount!==e.searchTextCount?e.searchTextCount-t.searchTextCount:t.hitCount!==e.hitCount?e.hitCount-t.hitCount:e.id-t.id});var a='<ul class="search-result-list">';o.forEach(function(t){a+=t.item}),a+="</ul>",s.innerHTML=a}};r.addEventListener("input",a),$(".local-search-pop-overlay").remove(),$("body").css("overflow",""),proceedsearch()}})};$(".popup-trigger").click(function(t){t.stopPropagation(),isfetched===!1?searchFunc(path,"local-search-input","local-search-result"):proceedsearch()}),$(".popup-btn-close").click(onPopupClose),$(".popup").click(function(t){t.stopPropagation()}),$(document).on("keyup",function(t){var e=27===t.which&&$(".search-popup").is(":visible");e&&onPopupClose()})</script></body><script>$(function(){$("a").not(".nav-link,.cloud-tie-join-count,.ignore-href,.jstree-anchor").addClass("animsition-link")});var burst1=new mojs.Burst({left:0,top:0,radius:{5:40},children:{shape:"circle",fill:["red","cyan","orange"],fillOpacity:.8,radiusX:3.5,radiusY:3.5}});document.addEventListener("click",function(a){null==a.target.href&&"footer"!=a.target.className&&"copyright"!=a.target.className&&"author__urls social-icons"!=a.target.className&&"author__avatar"!=a.target.className&&"sidebar sidebar-active"!=a.target.className&&burst1.tune({x:a.pageX,y:a.pageY}).generate().replay()})</script><script type="text/javascript" src="/js/message.js"></script></html><!-- rebuild by neat -->